package com.hoshiibuy.es.controller;

import com.hoshiibuy.es.entity.Student;
import com.hoshiibuy.es.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RequestMapping("/esClient")
@RestController
public class EsClientController {
    @Autowired
    private StudentService studentService;

    //添加学生信息
    @GetMapping("/addStudent")
    public String addStudent(String studentName, Integer age) {
        Student student = new Student(UUID.randomUUID().toString(),
                studentName, age, LocalDateTime.now());

        studentService.addStudent(student);

        return "success";
    }

    //批量添加学生信息
    @GetMapping("/batchAddStudent")
    public String batchAddStudent() {
        for (int i = 0; i < 100; i++) {
            Student student = new Student(UUID.randomUUID().toString(),
                    "黄家驹" + i, i + 1, LocalDateTime.now());
            studentService.addStudent(student);
        }

        return "success";
    }

    //根据学生姓名查询学生信息
    @GetMapping("/queryStudentsWithName")
    public List<Student> queryStudentsByName(String studentName) {
        return studentService.queryStudentsByName(studentName);
    }

    //创建es索引
    @GetMapping("/createIndex")
    public String queryStudentsByName() {
        studentService.createIndex();
        return "Success";
    }
}